package com.p7.notepad.network;

import android.util.Log;

import com.google.gson.Gson;
import com.p7.notepad.network.listener.ProgressListener;
import com.p7.notepad.network.listener.RequestListener;
import com.p7.notepad.network.support.RequestObserver;
import com.p7.notepad.network.support.UploadFileRequestBody;

import java.io.File;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.RequestBody;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.converter.scalars.ScalarsConverterFactory;

/**
 * Created by Administrator on 2017\11\28 0028.
 */
public class APIManager {

    OkHttpClient.Builder httpBuilder = new OkHttpClient.Builder();
    OkHttpClient client;
    private volatile static APIManager sInstance;
    private Retrofit retrofit;
    private APIServer server;

    private APIManager() {
        client = httpBuilder.readTimeout(30, TimeUnit.MINUTES)
                .connectTimeout(30, TimeUnit.MINUTES)
                .writeTimeout(30, TimeUnit.MINUTES)
                .build();

        retrofit = new Retrofit.Builder()
                .baseUrl("http://47.100.20.151:8092/notepad_api/app/")
                .client(client)
                .addConverterFactory(ScalarsConverterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build();

        server = retrofit.create(APIServer.class);
    }

    public static APIManager getInstance(){
        if (sInstance == null){
            synchronized(APIManager.class){
                if (sInstance == null){
                    sInstance = new APIManager();
                }
            }
        }
        return sInstance;
    }

    public APIServer getServer(){
        return server;
    }

    public static void onPost(String url, Map<String, String> parameters, final RequestListener listener) {
        Log.d("TAG", "接口：" + url);
        String json = new Gson().toJson(parameters);
        Log.d("TAG", "Json参数：" + json);
        RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), json);
        APIManager.getInstance().getServer()
                .Post(url, body)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new RequestObserver(listener));
    }

    public static void onPost(String url, Map<String, String> headers,
                              Map<String, String> parameters, final RequestListener listener) {

        Log.d("TAG", "接口：" + url);
        Log.d("TAG", "Header参数：" + headers.toString());
        if(parameters != null) {
            String json = new Gson().toJson(parameters);

            Log.d("TAG", "Json(parameters)参数：" + json);
            RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), json);

            APIManager.getInstance().getServer()
                    .Post(url, headers, body)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new RequestObserver(listener));
        } else {
            APIManager.getInstance().getServer()
                    .Post(url, headers)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new RequestObserver(listener));
        }
    }

    public static void uploadFile(String url, String path, final RequestListener listener, ProgressListener mListener) {

        Log.d("TAG", "接口：" + url);
        Log.d("TAG", "文件地址：" + path);
        File file = new File(path);
        RequestBody body =  RequestBody.create(MediaType.parse("multipart/form-data"), file);
        UploadFileRequestBody upload = new UploadFileRequestBody(body, mListener);
        MultipartBody.Part part = MultipartBody.Part.createFormData("file", file.getName(), upload);

        APIManager.getInstance().getServer()
                .uploadFile(url, part)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new RequestObserver(listener));
    }

   public static void uploadFile(String url, Map<String, String> headers, Map<String, String> parameters,
                                 String path, final RequestListener listener, ProgressListener mListener) {

        Log.d("TAG", "接口：" + url);
        Log.d("TAG", "请求头：" + headers);
        Log.d("TAG", "参数：" + parameters);
        Log.d("TAG", "文件地址：" + path);
        File file = new File(path);
        RequestBody body =  RequestBody.create(MediaType.parse("multipart/form-data"), file);
        UploadFileRequestBody upload = new UploadFileRequestBody(body, mListener);
        MultipartBody.Part part = MultipartBody.Part.createFormData("file", file.getName(), upload);

        APIManager.getInstance().getServer()
                .uploadFile(url, headers, parameters, part)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new RequestObserver(listener));
    }

}
